package es.aa.android.cartomatic.appview.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import es.aa.android.cartomatic.R;
import es.aa.android.cartomatic.domain.Card;

public class CardAdapter extends BaseAdapter {

    private Activity activity;
    private List<Card> cards;

    public CardAdapter(Activity activity, List<Card> cards) {
        this.activity = activity;
        this.cards = cards;
    }

    public void addCard(Card card) {
        this.cards.add(card);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return cards.size();
    }

    @Override
    public Object getItem(int position) {
        return cards.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            LayoutInflater lInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = lInflater.inflate(R.layout.card, null);
            
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.textViewTitle);
			holder.image = (ImageView) convertView.findViewById(R.id.imageView);
			holder.description = (TextView) convertView.findViewById(R.id.textViewSubtitle);
			
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        
        holder.title.setText(cards.get(position).getName());
        holder.image.setImageBitmap(cards.get(position).getImage());
        holder.description.setText(cards.get(position).getDescription());
        
        
        
        return convertView;
    }

    static class ViewHolder {
        private TextView title;
        private ImageView image;
        private TextView description;
    }

}
